from random import randint
import base64
import algorithm
import re


class AutoMain(object):
    def __init__(self):
        self.sf_ran = [str(randint(0, 3)) for i in range(0, 6)]  # 生成随机队列
        self.tail = "admin_tail=" + ''.join(self.sf_ran)

        self.sf_head = {
            '0': algorithm.B32Sf(),
            '1': algorithm.B16Sf(),
            '2': algorithm.B85Sf(),
            '3': algorithm.KaiSha(),
        }

    def b_encode(self, content: str) -> bytes:  # 初始化编码
        return base64.b64encode(content.encode())

    def b_decode(self, content: bytes) -> str:  # 初始化解吗
        return base64.b64decode(content).decode()

    def if_obj(self, flag):  # 判断使用的是哪一个加密器
        return self.sf_head[flag]

    def encryption(self, txt: str) -> str:  # 加密
        txt = self.b_encode(txt)  # 编码
        for i in self.sf_ran:  # 确定加密顺序
            obj = self.if_obj(i)
            # 加密
            txt = obj.get_content(txt)

        return txt.decode() + self.tail

    def decryption(self, txt: str) -> str:  # 解密
        txt = txt.encode()

        for i in self.sf_ran:  # 确定加密顺序
            obj = self.if_obj(i)

            txt = obj.put_content(txt)  # 解密

        txt = self.b_decode(txt)  # 解编码
        return txt

    def run(self, txt):
        # 获取数据头
        head = re.match(r".*?admin_tail=(\d+)", txt)

        if head:  # 匹配成功，加密体
            head = head.group(1)
            self.sf_ran = list(head[::-1])

            txt = re.sub(r"admin_tail=\d+$", '', txt)
            text = self.decryption(txt)  # 解密

            print(text)

        else:  # 匹配失败明文
            text = self.encryption(txt)  # 加密
            print(text)


if __name__ == '__main__':
    auto = AutoMain()
    a = input("输入加密或者解密的内容\n")
    auto.run(a)



